import org.omg.CORBA.Object;

import java.util.Arrays;

/**
 * @ClassName:
 * @author:
 * @Description:
 * @Date
 **/
//线段树
public class SegmentTree<E> {

    private E[] data;
    //存放线段树元素的数组
    private E[] tree;
    //合并接口
    private Merger<E> merger;

    public SegmentTree(E[] arr, Merger<E> merger) {
        data = (E[]) new Object[arr.length];
        this.merger = merger;
        for (int i = 0; i < arr.length; i++) {
            data[i] = arr[i];
        }
        tree = (E[]) new Object[4 * arr.length];
        buildSegmentTree(0, 0, data.length - 1);
    }

    //在treeIndex位置创建表示区间[l...r]的线段树
    private void buildSegmentTree(int treeIndex, int l, int r) {
        if (l == r) {
            tree[treeIndex] = data[l];
            return;
        }
        //左子树根
        int leftTreeIndex = leftChild(treeIndex);
        //右子树根
        int rightTreeIndex = rightChild(treeIndex);
        int mid = l + (r - l) / 2;
        //创建左子树 区间为[l,mid]
        buildSegmentTree(leftTreeIndex, l, mid);
        //创建右子树 区间为[mid+1,r]
        buildSegmentTree(rightTreeIndex, mid + 1, r);
        //父节点为左右子树的值合并后的结果（此处具体实现可以是max,min等等）
        tree[treeIndex] = merger.merge(tree[leftTreeIndex], tree[rightTreeIndex]);
    }

    public E get(int index) {
        if (index >= data.length || index < 0) {
            throw new IllegalArgumentException("index out of bound");
        }
        return data[index];
    }

    public int getSize() {
        return data.length;
    }

    public int leftChild(int index) {
        return 2 * index + 1;
    }

    public int rightChild(int index) {
        return 2 * index + 2;
    }


    //返回区间[queryL,queryR]的值
    public E query(int queryL, int queryR) {
        if (queryL < 0 || queryL > data.length || queryR < 0 || queryR >= data.length || queryL > queryR) {
            throw new IllegalArgumentException("index out of bound");
        }
        //
        return query(0, 0, data.length - 1, queryL, queryR);
    }

    //在以treeIndex为根的线段树中[l...r]的范围里，搜索区间[queryL...queryR]的值
    //一个节点表示一个区间，为l...r，queryL和queryR为查询的区间
    private E query(int treeIndex, int l, int r, int queryL, int queryR) {
        //如果左右区间正好和要搜索的区间相同直接返回当前节点
        if (l == queryL && r == queryR) {
            return tree[treeIndex];
        }
        int mid = l + (r - l) / 2;
        //左子树的索引
        int leftTreeIndex = leftChild(treeIndex);
        //右子树的索引
        int rightTreeIndex = rightChild(treeIndex);
        //如果没有左边
        if (queryL >= mid + 1) {
            return query(rightTreeIndex, mid + 1, r, queryL, queryR);
        } else if (queryR <= mid) {
            //如果没有右边
            return query(leftTreeIndex, l, mid, queryL, queryR);
        }
        E leftResult = query(leftTreeIndex, l, mid, queryL, mid);
        E rightResult = query(rightTreeIndex, mid + 1, r, mid + 1, queryR);
        //左右子树结果合并的值
        return merger.merge(leftResult, rightResult);
    }

    public void set(int index, E e) {
        if (index < 0 || index >= data.length) {
            throw new IllegalArgumentException("index out of bound");
        }
        data[index] = e;
        set(0, 0, data.length - 1, index, e);
    }

    //更新操作，更新区间后总体的结果也要变化
    private void set(int treeIndex, int l, int r, int index, E e) {
        if (l == r) {
            tree[treeIndex] = e;
            return;
        }
        int mid = l + (r - l) / 2;
        //左子树的索引
        int leftTreeIndex = leftChild(treeIndex);
        //右子树的索引
        int rightTreeIndex = rightChild(treeIndex);
        if (index >= mid + 1) {
            set(rightTreeIndex, mid + 1, r, index, e);
        } else {
            set(leftTreeIndex, l, mid, index, e);
        }
        tree[treeIndex] = merger.merge(tree[leftTreeIndex], tree[rightTreeIndex]);
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append('[');
        for (int i = 0; i < tree.length; i++) {
            if (tree[i] != null) {
                builder.append(tree[i]);
            } else {
                builder.append("null");
            }
            if (i != tree.length - 1) {
                builder.append(", ");
            }
        }
        builder.append("]");
        return builder.toString();
    }
}
